package hupu.hnlp.hot_buffer.front.configuration.rmi;

import javax.annotation.Resource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.remoting.rmi.RmiProxyFactoryBean;

import hupu.hnlp.hot_buffer.front.appconfig.AppConfig;
import hupu.hnlp.hot_buffer.share.rmi.distbuffer.DistBuffer;

@Configuration
public class MyRmiProxyConfiguration
{
    @Resource(name = "appconfig")
    private AppConfig appconfig;

    @Bean(name = "distBuffer0Proxy")
    public RmiProxyFactoryBean distBuffer0Proxy()
    {
        return create_proxy(appconfig.rmi.distbuffer.get(0));
    }

    @Bean(name = "distBuffer1Proxy")
    public RmiProxyFactoryBean distBuffer1Proxy()
    {
        return create_proxy(appconfig.rmi.distbuffer.get(1));
    }

    @Bean(name = "distBuffer2Proxy")
    public RmiProxyFactoryBean distBuffer2Proxy()
    {
        return create_proxy(appconfig.rmi.distbuffer.get(2));
    }

    @Bean(name = "distBuffer3Proxy")
    public RmiProxyFactoryBean distBuffer3Proxy()
    {
        return create_proxy(appconfig.rmi.distbuffer.get(3));
    }

    @Bean(name = "distBuffer4Proxy")
    public RmiProxyFactoryBean distBuffer4Proxy()
    {
        return create_proxy(appconfig.rmi.distbuffer.get(4));
    }

    @Bean(name = "distBuffer5Proxy")
    public RmiProxyFactoryBean distBuffer5Proxy()
    {
        return create_proxy(appconfig.rmi.distbuffer.get(5));
    }

    @Bean(name = "distBuffer6Proxy")
    public RmiProxyFactoryBean distBuffer6Proxy()
    {
        return create_proxy(appconfig.rmi.distbuffer.get(6));
    }

    @Bean(name = "distBuffer7Proxy")
    public RmiProxyFactoryBean distBuffer7Proxy()
    {
        return create_proxy(appconfig.rmi.distbuffer.get(7));
    }

    @Bean(name = "distBuffer8Proxy")
    public RmiProxyFactoryBean distBuffer8Proxy()
    {
        return create_proxy(appconfig.rmi.distbuffer.get(8));
    }

    @Bean(name = "distBuffer9Proxy")
    public RmiProxyFactoryBean distBuffer9Proxy()
    {
        return create_proxy(appconfig.rmi.distbuffer.get(9));
    }

    private RmiProxyFactoryBean create_proxy(String node)
    {
        RmiProxyFactoryBean proxy = new RmiProxyFactoryBean();
        proxy.setServiceUrl(String.format("rmi://%s/distBuffer", node));
        proxy.setServiceInterface(DistBuffer.class);
        proxy.setLookupStubOnStartup(false);
        proxy.setRefreshStubOnConnectFailure(true);
        proxy.afterPropertiesSet();
        return proxy;
    }
}
